package com.jike.jkgame.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AutoScrollView extends LinearLayout {

	private TextView mTextViews[] = new TextView[2];
	private int mNextIdx = 0;

	private String mText = null;
	private List<String> mLines = new ArrayList<String>();
	private Rect mTextBound = new Rect();
	private Handler mHandler = new Handler();
	private static final int LINE_MARGIN = 1;
	private int mNumLine = 0;
	private int mWidth = 0;
	private int mHeight = 0;
	private int mScroll = 0;
	private LinearLayout.LayoutParams mLayoutParms;

	public AutoScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public AutoScrollView(Context context) {
		super(context);
		init(context, null);
	}

	private void init(Context context, AttributeSet attrs) {
		setOrientation(LinearLayout.VERTICAL);

		mTextViews[0] = new TextView(context, attrs);
		mTextViews[0].setSingleLine(true);
		mTextViews[1] = new TextView(context, attrs);
		mTextViews[1].setSingleLine(true);

		mLayoutParms = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		mLayoutParms.topMargin = LINE_MARGIN;
		mLayoutParms.bottomMargin = LINE_MARGIN;

		addView(mTextViews[0], mLayoutParms);
		addView(mTextViews[1], mLayoutParms);

	}

	private int measureViewWidth(View view) {
		ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) getLayoutParams();
		if (params == null) {
			params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT);
		}

		int widthSpec = ViewGroup.getChildMeasureSpec(0, 0, params.width);
		int heightSpec;
		int height = params.height;
		if (height > 0) {
			heightSpec = MeasureSpec.makeMeasureSpec(height,
					MeasureSpec.EXACTLY);
		} else {
			heightSpec = MeasureSpec.makeMeasureSpec(height,
					MeasureSpec.UNSPECIFIED);
		}
		measure(widthSpec, heightSpec);

		return getMeasuredWidth();
	}

	private void splitLines() {

		mLines.clear();
		mNumLine = 0;
		if (mText == null || mWidth == 0) {
			return;
		}

		TextPaint textPaint = mTextViews[0].getPaint();

		int len = mText.length();

		int idx = 0;

		do {
			int size = textPaint.breakText(mText, idx, len, true, mWidth, null);

			mLines.add(mText.substring(idx, Math.min(len, idx + size)));
			idx += size;
		} while (idx < len);

		mNumLine = mLines.size();
		mNextIdx = 0;
	}

	public void setText(String text) {
		mText = text;
		splitLines();
		startMarquee();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = mTextViews[0].getMeasuredWidth();
		mHeight = mTextViews[0].getMeasuredHeight();

		setMeasuredDimension(mWidth, mHeight);

		splitLines();
		startMarquee();
	}

	public void startMarquee() {
		mHandler.removeCallbacks(mUpdateResults);
		mHandler.post(mUpdateResults);
	}

	Runnable mUpdateResults = new Runnable() {
		@Override
		public void run() {

			mHandler.removeCallbacks(mUpdateResults);
			if (mNumLine == 0) {
				return;
			}
			
			scrollTo(0, mScroll);
			mScroll += 1;
			long delay = 50;
			if (mScroll >= mHeight + LINE_MARGIN) {
				TextView tv = (TextView) getChildAt(0);
				removeViewAt(0);
				tv.setText(mLines.get(mNextIdx));
				addView(tv, mLayoutParms);

				mNextIdx++;
				if (mNextIdx == mNumLine) {
					mNextIdx = 0;
				}

				mScroll = 0;
				scrollTo(0, 0);
				delay = 5000;
			}

			if (mNumLine > 1) {
				mHandler.postDelayed(mUpdateResults, delay);
			}

		}

	};
}
